﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace affmanager {
    class HandleData {

        //Checks if data is an correct integer
        private static int TryToParseInt(string value) {
            int number;
            bool result = Int32.TryParse(value, out number);

            if (result == true) {
                return number;
            }
            else {
                throw new ArgumentException("Could'nt convert the value to an integer!");
            }
        }

        //Loading all the data from the datafile
        public static bool LoadAllSites(out List<Sites> data, string dataFile) {
            data = new List<Sites>();

            if(!File.Exists(dataFile)) { return false; }

            StreamReader read = new StreamReader(dataFile);
            while (!read.EndOfStream) { //Read all lines
                string[] str = read.ReadLine().Split('|'); // Temp string
                string name = str[0];
                string url = str[1];
                string pagerank = str[2];
                string age = str[3];

                data.Add(new Sites(name, url, pagerank, age, "0", "0", "0"));
            }

            return true;
        }

        public static void addNewSite(out List<Sites> data,string name, string url, string PR, string age) {
            data = new List<Sites>();

            string _name = name;
            string _url = url;
            string _pagerank = PR;
            string _age = age;

            data.Add(new Sites(_name, _url, _pagerank, _age, "0", "0", "0"));
        }

        //Writing data to the datafile
        public static void WriteAllSites(List<Sites> data, string dataFile) {

        }
    }
}
